/**
 * 投放广告的 管理 页面
 * 包括列表，投放表单等；
 */
var Options = window.Options || {};
var PlaceAPI = window.PlaceAPI || {};

var vue = new Vue({
    el: '#VueApp',
    delimiters: ['${', '}'],
    data: function () {
        return {
            keyword: "",
            delDisabled: true, //删除禁用
            getCodeDisabled: true, //获取代码
            stopDisabled: true, //停止 禁用
            startDisabled: true, //开始 禁用

            showPlaceSearch: false, //不显示筛选框
            drawerNewPlace: false, //新增广告位的抽屉drawer
            submit:'',
            tableLoading: false,

            queryParams: {},// 搜索参数

            page:1,
            total: 0,//总记录数
            pageSize: 10,//分页数据量
            tablelist: [], //表数据
            multipleSelection: [],//被选中的行
            // 编辑或复制
            idAdverte:0,
            idCopy:0,
        }
    },
    created() {
        this.getListInfo();
    },
    filters: {
        "filter_adtype" : Options.filter_adtype ,
        "filter_placeType" :Options.filter_placeType ,
        "filter_caculateMethod": Options.filter_caculateMethod,

        filter_disabled(value) {
            var label = { "0": "启用", "1": "禁用", "9": "删除" };
            return label[value];
        },
        filter_modifytime(value) {
            return value.split(" ")[0];
        }
    },

    methods: {
        OnShowPlaceSearch() {
            this.showPlaceSearch = this.showPlaceSearch ? false : true;
            this.$refs.dropIcon.className = this.showPlaceSearch ? " el-icon-caret-bottom" : "el-icon-caret-top";
        },
        //关键词搜索
        onKeywordChange(value) {
            this.queryParams['keyword'] = this.keyword;
            this.getListInfo()
        },
        //条件搜索窗的 回调 <set-place-search> 组件 事件 onselectchange
        onSelectChange(e) {
            // console.log(e);
            this.page = 1;
            this.queryParams = e;
            this.queryParams['keyword'] = this.keyword;
            this.getListInfo()
        },
        openNewPlaceDrawer(){
            this.drawerNewPlace = true;
            this.idAdverte = 0;
            this.idCopy = 0;
        },
        // 表单组件提交表单 后成功，关闭抽屉
        onCloseDrawer(e) {
            // console.log(e)
            //e == { from: "submit"}
            this.submit = e.from;
            this.$refs.newPlaceDrawer.closeDrawer();
            this.getListInfo();
            /* 
            closeDrawer 方法会先调用  
            下面的 handleDrawerClose 方法进行关闭
             因为设置了 before-close 属性
             所以通过设置一个 submit 变量进行判断 关闭 来自 关闭按钮，还是表单提交后。
             */
        },
        // 点击抽屉右上角 关闭按钮，关闭新建广告位的drawer
        handleDrawerClose(done) {
            // console.log(this.submit)
            if (this.submit == 'submit') {
                done();
                return;
            }
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        handlePageSize(size) {
            this.pageSize = size;
            this.getListInfo()
        },
        handleCurrentChange(page) {
            this.page = page;
            this.getListInfo()


        },
        //全新勾选
        handleSelectionChange(row) {
            this.multipleSelection = row;
        },
        getKeyword() {
            this.page = 1;
            this.getListInfo()
        },
        // 编辑 按钮 点击事件
        handleEdit(index, row) {
            this.idAdverte = row;
            this.idCopy = 0;
            this.drawerNewPlace = true;
            
        },
        handleDelete(index, row) {
           
            this.$confirm('此操作将永久删除数据, 是否继续?', '提示').then(() => {
                PlaceAPI.deleteAdverte(row.id).then((ret) => {
                    this.$message({ message: ret.msg })
                    this.tablelist.splice(index, 1);
                    --this.total;
                })
            }).catch((e) => { console.log(e) });
        },
        handleCopy(index, row){
            this.idCopy = row;
            this.idAdverte = 0;
            this.drawerNewPlace = true;
        },

        handleCommand(scope){
            // console.log(scope)
            if (scope.action == "delete") {
                this.handleDelete(scope.index, scope.row);
            } else if (scope.action == "copy"){
                this.handleCopy(scope.index, scope.row);
            } else if (scope.action == "chart"){
                // console.log(scope);
                let href = window.location.href;
                window.location.href = href.replace('setplace', 'adverteReport') + "&idadverte="+ scope.row.id;
            }else{}
               
                
        },
        //获取列表数据
        getListInfo() {
            this.tableLoading = true;
            PlaceAPI.getAllsAdvertes(this.page, this.pageSize, this.queryParams).then((data) => {
                this.tableLoading = false;
                // for(var i in data){
                // 把必须要的参数赋值进去
                //data[i]['adtypeOptions'] = ;
                //  }
                this.total = parseInt(data.total);
                this.tablelist = data.data;
                // console.log(data)
            })
        },
    }
})

